/******************** (C) COPYRIGHT 2011 STMicroelectronics ********************
* File Name          : main.c
* Author             : MCD Application Team
* Version            : V3.3.0
* Date               : 21-March-2011
* Description        : Custom HID demo main file
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/

#ifndef	APP_USB_GLOBALS
#define	APP_USB_GLOBALS
#include <includes.h>
#include "app_usb.h"
#include "usb_pwr.h"
#include "communi_driver.h"
#include "communi_server.h"
#include "hw_config.h"
#include "usb_desc.h"
#include "usb_lib.h"


/*******************************************************************************
* Function Name  : EP1_OUT_Callback.
* Description    : EP1 OUT Callback Routine.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
static void USBCommRxDataResolve(void);
static uint8_t *GetUSBCommPackage(uint8_t *Point,uint16_t *ByteNum,uint8_t DevID,uint8_t *Status);
	
void EP1_OUT_Callback(void)
{
	uint16_t   CaptureValue;
	
	/* Time Overflow counting 30ms */
	CaptureValue = TIM_GetCnt(CommuniTIM);
	TIM_SetCmp2(CommuniTIM, CaptureValue + USBTIM_Pulse);
	TIM_ClrIntPendingBit(CommuniTIM, USBTIM_IT_CCx);
	TIM_ConfigInt(CommuniTIM, USBTIM_IT_CCx, ENABLE);
	
	
	/* Max Limit */
	if(USBRXBufferData[USBRXBufferDataTOTNum-1]==COMMRecdataType_Null)
	if((USBRXBufferCount+HIDMaxPacketSize)<USBRXBufferDataTOTNum)
	{
		USB_SilRead(EP1_OUT,&USBRXBufferData[USBRXBufferCount]);   /* Read received data (64 bytes) */ 
		USBRXBufferCount+=HIDMaxPacketSize;
	}
	
#ifndef STM32F10X_CL   
  	SetEPRxStatus(ENDP1, EP_RX_VALID);
#endif /* STM32F10X_CL */
  
	/* Data Resolve */
	if((USBRXBufferData[USBRXBufferDataTOTNum-1]==COMMRecdataType_Null)&&(USBRXBufferCount))
		USBCommRxDataResolve();
}


/* USB Rx Data Resolve */
static void USBCommRxDataResolve(void)
{
	uint8_t *Point1,*Point2,DevID,Status,i;
	
	/* Get DeviceID */
	// DevID = (uint8_t)GetDataBaseDataContent(FakeConfig_ModuleID);  //中断中不允许等待信号量
	DevID = 0x01;  //临时配置
	Point1 = USBRXBufferData;
	
	/* Cycle Resolve */
	for(Status=USBPackageSta_ErorFun; Status==USBPackageSta_ErorFun;)
	{
	  Point2=GetUSBCommPackage(Point1, &USBRXBufferCount, DevID, &Status);
	  switch(Status)
	  {
      case USBPackageSta_Invaild:
      	   USBRXBufferCount=0;
      	   break;
      case USBPackageSta_PartMsg:
      	   if(Point2>Point1)
      	     	for(i=0;i<USBRXBufferCount;i++)
      	       		USBRXBufferData[i]=*(Point2++);
      	   break;
      case USBPackageSta_LongPG :
      	   if(USBRXBufferData[USBRXBufferDataTOTNum-1]==COMMRecdataType_Null)
      	   {
      	   	 	USBRXBufferData[USBRXBufferDataTOTNum-1]=COMMRecdataType_Full;
      	   	 
      	   	 	uint8_t os_err;
      	   	 	OSFlagPost(App_Flags_COMMUNI,OSCOMMUNIFlags_USBRXEnd,OS_FLAG_SET,&os_err);
      	     
      	     	USBRXBufferCount=0;
      	   }
      	   break;
	  }
	  Point1=Point2;
  }
}



/* Get Package */
static uint8_t *GetUSBCommPackage(uint8_t *Point,uint16_t *ByteNum,uint8_t DevID,uint8_t *Status)
{
	uint16_t TempNum,TempCal;
	
	TempNum=*ByteNum;
	*Status=USBPackageSta_Invaild;
	
	/* ID match */
	for(;TempNum>0;TempNum--,Point++)
		if((*Point==DevID)||(*Point==0x00 && *(Point+1)==0x06))
		  	break;
	
	if(TempNum==0)
	  	return(NULL);
	
	Point++;
	/* Error Function Code */
	if((*Point<USBCommFunctionCodeRange_Min) || (*Point>USBCommFunctionCodeRange_Max))
	{
		*Status=USBPackageSta_ErorFun;
	  	return(Point);
	}
	
	/* Long package (design by RAM)*/
	TempCal =*(++Point);
	TempCal<<=8;
	TempCal+=*(++Point);
	
	TempCal+=6;
	if(TempNum<TempCal)
	{
		*ByteNum=TempNum;
		*Status=USBPackageSta_PartMsg;
		return(Point-3);
	}
	
	*ByteNum=TempNum-TempCal;
	*Status=USBPackageSta_LongPG;
	return(Point+TempCal-3);
}


/******************************************/
/******************************************/


static void USB_RAMInit(void);
static void USB_RAMInit(void)
{
	PrevXferComplete=DEF_FALSE;
	
	bDeviceState = UNCONNECTED; /* USB device status */
	fSuspendEnabled = true;  /* true when suspend is possible */
}


static void USB_HWInit();
static void USB_HWInit()   //为了防止干扰
{
	GPIO_InitType GPIO_InitStructure;
	
	GPIO_InitStruct(&GPIO_InitStructure);
    GPIO_InitStructure.Pin = GPIO_PIN_10;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
    GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
	GPIO_ResetBits(GPIOA, GPIO_PIN_10);
}


/* Initial USB interface */
void COMMUNI_HWInit_USB(void)
{
	USB_RAMInit();
	
  	Set_System();
	if(USB_Config(SYSCLK_VALUE_144MHz) == SUCCESS)
  	{
    	USB_Init();
		// USB_HWInit();
//    while (bDeviceState != CONFIGURED)
//    {
//    }
  	}

  	USB_Interrupts_Config();
}





#endif	/*	APP_USB_GLOBALS	*/

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
